//
//  DXHomeTableViewCell.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/10.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
import SnapKit
class DXHomeTableViewCell: UITableViewCell {
    
    var toolBarTopConstraint:Constraint?
    
//     viewmodel模型
    var statuesVeiwModel:DXStatuesViewModel?{
        didSet{
        originalStatuesView.statuesViewModel = statuesVeiwModel
        toobarView.statuesViewModel = statuesVeiwModel
             toolBarTopConstraint?.uninstall()
            
            if (statuesVeiwModel!.hasRetweeted_status) {
                retweetedView.statuesViewModel = statuesVeiwModel
//            存在转发微博
                retweetedView.hidden = false
                toobarView.snp_updateConstraints(closure: { (make) -> Void in
                  toolBarTopConstraint =  make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
            }else{
               retweetedView.hidden = true
                toobarView.snp_updateConstraints(closure: { (make) -> Void in
                  toolBarTopConstraint =  make.top.equalTo(originalStatuesView.snp_bottom).constraint
                })
            
            }
                      
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//       初始化UI
        setUpUI()
        
        contentView.backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    初始化UI
    private func setUpUI(){
    
//        添加子控件
        contentView.addSubview(originalStatuesView)
        
        contentView.addSubview(toobarView)
        
        contentView.addSubview(retweetedView)
//        子控件添加约束原生微博
        originalStatuesView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(10)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
//        转发微博
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalStatuesView.snp_bottom)
            make.leading.equalTo(originalStatuesView)
            make.trailing.equalTo(originalStatuesView)
        }
//        toolBarView
        toobarView.snp_makeConstraints { (make) -> Void in
            
        let constraint = make.top.equalTo(retweetedView.snp_bottom).constraint
            
          toolBarTopConstraint = constraint
            make.leading.equalTo(originalStatuesView)
            make.trailing.equalTo(originalStatuesView)
            make.height.equalTo(35)
        }
        
        
//        cell自身约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toobarView)
        }
        
        
    
    }
//    懒加载控件
    let originalStatuesView:DXstatuesOriginalView = DXstatuesOriginalView()
    let toobarView :DXToobarView = DXToobarView()
    let retweetedView :DXRetweetedView = DXRetweetedView()

}
